﻿/*

The MIT License

Copyright (c) 2010 Cartesian Analytics, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Reflection;
using System.Xml;

namespace Pipra.Uom.Epsg
{

    public class EpsgUom :
        Uom
    {

        public static EpsgUom GetUnit(int code)
        {
            return EpsgUomGraph.Default.GetUnit(code);
        }

        public static EpsgUom GetUnit(string type, string name)
        {
            return EpsgUomGraph.Default.GetUnit(type, name);
        }

        public readonly int Code;

        public EpsgUom(string name, int code, IUomAuthority authority)
            :base(name,authority)
        {
            Code = code;
        }

    }

    /*
    /// <summary>
    /// An EPSG unit of measure.
    /// </summary>
    public class EpsgUom :
        IUom
    {

        private class EpsgUomIdLookup : KeyedCollection<int, EpsgUom>
        {
            protected override int GetKeyForItem(EpsgUom item)
            {
                return item.Code;
            }
        }

        private class EpsgUomNameLookup : KeyedCollection<string, EpsgUom>
        {
            public EpsgUomNameLookup(UomComparer cmp) : base(cmp) { }

            protected override string GetKeyForItem(EpsgUom item)
            {
                return item.Name;
            }
        }

        private class EpsgUomType
        {
            public readonly string Type;
            private readonly EpsgUomIdLookup _idLookup;
            private readonly EpsgUomNameLookup _nameLookup;

            public EpsgUomType(string name, UomComparer cmp)
            {
                if(String.IsNullOrEmpty(name))
                {
                    throw new ArgumentNullException();
                }
                Type = name;
                _idLookup = new EpsgUomIdLookup();
                _nameLookup = new EpsgUomNameLookup(cmp);
            }

            public void Add(EpsgUom uom)
            {
                _idLookup.Add(uom);
                _nameLookup.Add(uom);
            }

            public EpsgUom GetById(int id)
            {
                return _idLookup.Contains(id) ? _idLookup[id] : null;
            }

            public EpsgUom GetByName(string name)
            {
                return _nameLookup.Contains(name) ? _nameLookup[name] : null;
            }

        }

        private class EpsgUomTypeCollection : KeyedCollection<string, EpsgUomType>
        {
            protected override string GetKeyForItem(EpsgUomType item)
            {
                return item.Type;
            }
        }

        private const string UnitCodesResourceName = "Pipra.Uom.Epsg.UnitCodes.xml";
        private static readonly EpsgUomTypeCollection Lookup;

        static EpsgUom()
        {
            Lookup = ParseXmlToLookup(Assembly.GetAssembly(typeof (EpsgUom)).GetManifestResourceStream(UnitCodesResourceName),new UomComparer());
        }

        private static EpsgUomTypeCollection ParseXmlToLookup(Stream codeXmlStream, UomComparer cmp)
        {
            var lookup = new EpsgUomTypeCollection();
            using (var reader = new XmlTextReader(codeXmlStream))
            {
                reader.MoveToContent();
                while (reader.Read())
                {
                    if (reader.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }
                    reader.MoveToAttribute("n");
                    string uomTypeName = reader.Value;
                    reader.MoveToElement();

                    EpsgUomType uomType;
                    if (!lookup.Contains(uomTypeName) || (null == (uomType = lookup[uomTypeName])))
                    {
                        uomType = new EpsgUomType(uomTypeName, cmp);
                        lookup.Add(uomType);
                    }

                    using (var unitReader = reader.ReadSubtree())
                    {
                        unitReader.Read();
                        while (unitReader.Read())
                        {
                            if (reader.NodeType != XmlNodeType.Element)
                            {
                                continue;
                            }
                            reader.MoveToAttribute("n");
                            string uomId = reader.Value;
                            reader.MoveToAttribute("c");
                            int code = Int32.Parse(reader.Value);
                            uomType.Add(new EpsgUom(uomId, code));
                        }
                    }

                }
            }
            return lookup;
        }

        /// <summary>
        /// Gets an EPSG unit by its type and name.
        /// </summary>
        /// <param name="type">The type of unit to find.</param>
        /// <param name="name">The name of the unit to find.</param>
        /// <returns>An EPSG unit; null on failure.</returns>
        public static EpsgUom GetUnit(string type, string name)
        {
            return Lookup.Contains(type) ? Lookup[type].GetByName(name) : null;
        }
        /// <summary>
        /// Gets an EPSG unit by its EPSG code.
        /// </summary>
        /// <param name="code">The EPSG code.</param>
        /// <returns>An EPSG unit; null on failure.</returns>
        public static EpsgUom GetUnit(int code)
        {
            foreach(EpsgUomType type in Lookup)
            {
                EpsgUom uom = type.GetById(code);
                if(null != uom)
                {
                    return uom;
                }
            }
            return null;
        }

        /// <summary>
        /// The name.
        /// </summary>
        public readonly string Name;
        /// <summary>
        /// The EPSG code.
        /// </summary>
        public readonly int Code;
        /// <summary>
        /// The UoM authority.
        /// </summary>
        public readonly IUomAuthority Authority;

        /// <summary>
        /// Constructs a new EPSG unit.
        /// </summary>
        /// <param name="name">The name of the unit.</param>
        /// <param name="code">The EPSG code for the unit.</param>
        internal EpsgUom(string name, int code, IUomAuthority authority)
        {
            if(String.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Name is invalid.");
            }
            if(null == authority)
            {
                throw new ArgumentNullException("authority");
            }
            Name = name;
            Code = code;
        }

        string IUom.Name
        {
            get { return Name; }
        }



        public string Type
        {
            get { throw new NotImplementedException(); }
        }
    }*/
}
